<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Sets socket options for the socket</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.socket-set-option" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_set_option</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">socket_set_option</span> &mdash; <span class="dc-title">Sets socket options for the socket</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-set-option-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>socket_set_option</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$level</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$optname</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$optval</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   The <span class="function"><strong>socket_set_option()</strong></span> function sets the option
   specified by the <code class="parameter">optname</code> parameter, at the
   specified protocol <code class="parameter">level</code>, to the value pointed to
   by the <code class="parameter">optval</code> parameter for the 
   <code class="parameter">socket</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-set-option-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">socket</code></dt>

     <dd>

      <p class="para">
       A valid socket resource created with <span class="function"><a href="socket_create.html" class="function">socket_create()</a></span>
       or <span class="function"><a href="socket_accept.html" class="function">socket_accept()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">level</code></dt>

     <dd>

      <p class="para">
       The <code class="parameter">level</code> parameter specifies the protocol
       level at which the option resides. For example, to retrieve options at
       the socket level, a <code class="parameter">level</code> parameter of
       <strong><code>SOL_SOCKET</code></strong> would be used. Other levels, such as
       TCP, can be used by specifying the protocol number of that level. 
       Protocol numbers can be found by using the 
       <span class="function"><a href="getprotobyname.html" class="function">getprotobyname()</a></span> function.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">optname</code></dt>

     <dd>

      <p class="para">
       The available socket options are the same as those for the
       <span class="function"><a href="socket_get_option.html" class="function">socket_get_option()</a></span> function.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">optval</code></dt>

     <dd>

      <p class="para">
       The option value.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.socket-set-option-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.socket-set-option-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5608">
    <p><strong>Example #1 <span class="function"><strong>socket_set_option()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br /><br />if&nbsp;(!</span><span style="color: #0000BB">is_resource</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Unable&nbsp;to&nbsp;create&nbsp;socket:&nbsp;'</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">())&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(!</span><span style="color: #0000BB">socket_set_option</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_SOCKET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SO_REUSEADDR</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Unable&nbsp;to&nbsp;set&nbsp;option&nbsp;on&nbsp;socket:&nbsp;'</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">())&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(!</span><span style="color: #0000BB">socket_bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1223</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Unable&nbsp;to&nbsp;bind&nbsp;socket:&nbsp;'</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">())&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$rval&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_get_option</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_SOCKET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SO_REUSEADDR</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$rval&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Unable&nbsp;to&nbsp;get&nbsp;socket&nbsp;option:&nbsp;'</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">())&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;if&nbsp;(</span><span style="color: #0000BB">$rval&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'SO_REUSEADDR&nbsp;is&nbsp;set&nbsp;on&nbsp;socket&nbsp;!'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.socket-set-option-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="socket_create.html" class="function" rel="rdfs-seeAlso">socket_create()</a> - 创建一个套接字（通讯节点）</span></li>
    <li class="member"><span class="function"><a href="socket_bind.html" class="function" rel="rdfs-seeAlso">socket_bind()</a> - 给套接字绑定名字</span></li>
    <li class="member"><span class="function"><a href="socket_strerror.html" class="function" rel="rdfs-seeAlso">socket_strerror()</a> - Return a string describing a socket error</span></li>
    <li class="member"><span class="function"><a href="socket_last_error.html" class="function" rel="rdfs-seeAlso">socket_last_error()</a> - Returns the last error on the socket</span></li>
    <li class="member"><span class="function"><a href="socket_get_option.html" class="function" rel="rdfs-seeAlso">socket_get_option()</a> - Gets socket options for the socket</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>